package com.fare.taluo.application.impl.system.auth;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fare.taluo.application.system.auth.UserInfoLogApplication;
import com.fare.taluo.auth.domain.UserInfoLogEntity;
import com.fare.taluo.auth.security.LogonUserCertificate;
import com.fare.taluo.base.ddd.application.BaseApplicationImpl;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.vo.auth.UserInfoLoginVO;
@Transactional
@Service("userInfoLogApplication")
@Scope
public class UserInfoLogApplicationImpl extends BaseApplicationImpl implements UserInfoLogApplication{

	@Override
	public String releaseSession() {
		
		
		
		return null;
	}

	@Override
	public UserInfoLoginVO checkUserLogin(String userName) {
		UserInfoLogEntity entity=new UserInfoLogEntity();
		if(userName !=null && !userName.equals("")){
			entity=entity.checkUserNmaeLogin(userName);
			if(entity !=null){
				return entityInputVO(entity);
			}
		}
		
		return null;
	}

	public UserInfoLoginVO entityInputVO(UserInfoLogEntity entity){
		UserInfoLoginVO vo=new UserInfoLoginVO();
		vo.setCreateTime(entity.getCreateTime()!=null?entity.getCreateTime():null);
		vo.setCreatTimeStr(entity.getCreateTime()!=null?DateUtils.dateToStr(entity.getCreateTime(), "yyyy-MM-dd HH:mm:ss"):null);
		vo.setLoginTime(entity.getLoginTime() !=null?entity.getLoginTime():null);
		vo.setLoginTimeStr(entity.getLoginTime() !=null?DateUtils.dateToStr(entity.getLoginTime(), "yyyy-MM-dd HH:mm:ss"):null);
		vo.setLogoutTime(entity.getLogoutTime()!=null?entity.getLogoutTime():null);
		vo.setLogoutTimeStr(entity.getLogoutTime()!=null?DateUtils.dateToStr(entity.getLogoutTime(), "yyyy-MM-dd HH:mm:ss"):null);
		vo.setOnlineTimeStr(entity.getOnlineTime() !=null?entity.getOnlineTime():null);
		vo.setUserName(entity.getUserName());
		vo.setIP(entity.getIP());
		vo.setId(entity.getId());
		
		return vo;
	}
	public UserInfoLogEntity voInputEntity(UserInfoLoginVO vo){
		UserInfoLogEntity entity=new UserInfoLogEntity();
		entity.setCreateTime(vo.getCreateTime()!=null?vo.getCreateTime():null);
		entity.setLoginTime(vo.getLoginTime() !=null?vo.getLoginTime():null);
		entity.setLogoutTime(vo.getLogoutTime()!=null?vo.getLogoutTime():null);
		entity.setUserName(vo.getUserName());
		if(vo.getLoginTime() !=null && vo.getLogoutTime() !=null){
			entity.setOnlineTime(DateUtils.intervalTimeStr(vo.getLogoutTime(), vo.getLoginTime()));
		}else{
			entity.setOnlineTime(null);
		}
		
		entity.setIP(vo.getIP());
		entity.setId(vo.getId() !=null?vo.getId():null);
		
		return entity;
	}

	@Override
	public void saveOrUpdate(UserInfoLoginVO vo) {
		UserInfoLogEntity entity =new UserInfoLogEntity();
		if(vo !=null){
			entity=voInputEntity(vo);
			entity.saveOrUpdate(entity);
		}
		
	}

	@Override
	public int countUserOnline() {
		UserInfoLogEntity entity =new UserInfoLogEntity();
		return entity.countUserOnline();
	}

	@Override
	public void systemUpdate() {
		UserInfoLogEntity entity =new UserInfoLogEntity();
		entity.systemUpdate();
	}

	
	@Override
	public List<UserInfoLoginVO> findAll(String loginTimeStr,String logoutTimeStr) {
		UserInfoLogEntity ep =new UserInfoLogEntity();
		List<UserInfoLoginVO> list =new ArrayList<UserInfoLoginVO>();
		List<UserInfoLogEntity> listEntity=new ArrayList<UserInfoLogEntity>();
		Date loginTime=null,logoutTime=null;
		if(loginTimeStr !=null &&  loginTimeStr !=""){
			loginTime=DateUtils.StringToDate("yyyy-MM-dd HH:mm:ss", loginTimeStr);
		}
		if(logoutTimeStr !=null && logoutTimeStr !=""){
			logoutTime=DateUtils.StringToDate("yyyy-MM-dd HH:mm:ss", logoutTimeStr);
		}	
		listEntity=ep.findAll(loginTime,logoutTime);
		if(listEntity.size()>0){
			for(UserInfoLogEntity entity:listEntity){
				UserInfoLoginVO vo=new UserInfoLoginVO();
				vo=this.entityInputVO(entity);
				list.add(vo);
			}
		}
		return list;
	}

	@Override
	public void updateVo(LogonUserCertificate user) {
		if (user != null && user.getLoginId() != null && !user.getLoginId().equals("")) {
			// 表示用户“退出”，则保存用户信息
			String userName = user.getLoginId();
			UserInfoLoginVO vo = new UserInfoLoginVO();
			vo = this.checkUserLogin(userName);
			if (vo != null) {
				if (vo.getLogoutTimeStr() == null && vo.getOnlineTimeStr() == null) {
					vo.setLogoutTime(new Date());
					this.saveOrUpdate(vo);
				}
			}
		}
	}

}
